//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}
//加载字典
function dictJudge(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.options.sex;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}

//图片审批
function processUpdate(id){
    $('#pic_process').modal('show');

    vm.processView.shootId = id;

    //获取图片
    $.get(baseURL + "picApproval/mediafile/listByShootId/" + id, function (result) {
        console.log(result);
        var mList = result.mediaFileList;
        if (mList && mList.length > 0) {
            var htmls = '';
            for (var i = 0; i < mList.length; i++) {
                htmls += '<div class="col-sm-3 col-md-4">' +
                    '<div class="thumbnail">' +
                    '<img src="' + mList[i].mediaUrl + '"ondblclick="javascript:window.open(this.src);" >' +
                    '<div style="display:none"><h3><input type="checkbox" id="c' + i + '"class="form-control input-sm" value="' + mList[i].mediaId + '"/></h3></div></div></div>';
            }
            // var htmls='<img src="'+result.mediaFile.mediaUrl+'"ondblclick="javascript:window.open(this.src);" >';
            $('#pic_unapproved').html(htmls);

        } else {
            $('#pic_unapproved').html('<p  style="margin:20px;font-size:20px">未提交图片</p>');
        }
    })
}


$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'picApproval/shoot/list',
        datatype: "json",
        colModel: [			
			{ label: '编号', name: 'shootId', index: 'SHOOT_ID', width: 50, key: true },
			{ label: '地址', name: 'shootAddress', index: 'SHOOT_ADDRESS', width: 80 },
			{ label: '用户名', name: 'userName', index: 'USER_NAME', width: 40 },
			{ label: '审核状态', name: 'processStatus', index: 'PROCESS_STATUS', width: 30,
				formatter: function (cellvalue, options, rowObject) {
					if (cellvalue == 0){
						return "未审核";
					}
                    if (cellvalue == 1){
                        return "审核通过";
                    }
                    if (cellvalue == 2){
                        return "审核不通过";
                    }
                    if (cellvalue == 3){
                        return "初始化";
                    }
                }
			},
			{ label: '审核员编号', name: 'reviewerId', index: 'REVIEWER_ID', width: 30 },
			{ label: '随拍类型', name: 'shootType', index: 'SHOOT_TYPE', width: 30,
					formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == 1){
                            return "随手拍"
                        }
                        if (cellvalue == 2){
                            return "快速挪车"
                        }
                    }
			},
			{ label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 50,
					formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == null){
                            return "";
                        } else {
                            return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellvalue));
                        }
					}
			},
			{ label: '审核时间', name: 'checkTime', index: 'CHECK_TIME', width: 50,
                    formatter: function (cellvalue, options, rowObject) {
						if (cellvalue == null){
							return "";
						} else {
							return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellvalue));
                        }
                    }
			},
            { label: '操作', name: '', index: '', width:25 ,
                formatter: function (cellvalue, option, rowObject) {
                        var a = '';
                        if (rowObject.processStatus  == '1' || rowObject.processStatus == '2'){   //若已经审核通过/不通过，不可二次审核
                            a = 'disabled';
                        }
                    return '&nbsp;&nbsp;<button class="btn btn-info btn-xs" type="button"  ' + a + ' onclick=processUpdate(\"' + rowObject.shootId + '\")>审批</button>';
                }
            }
			// { label: '经度', name: 'locationX', index: 'LOCATION_X', width: 80 },
			// { label: '纬度', name: 'locationY', index: 'LOCATION_Y', width: 80 },
			// { label: '多媒体文件编号', name: 'mediaId', index: 'MEDIA_ID', width: 80 },
			// { label: '更新时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        postData : {
            'shootType': '1'     //初始化只搜索 随手拍 类别的
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            processStatus:'',   //审批状态
            shootType:'1',
            userName:''
		},
		showList: true,
		title: null,
		shoot: {},
        processView:{  //审批悬浮框对应的shootId
            shootId:'',
            processStatus:'2'  //审批是否通过
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.shoot = {};
		},
		update: function (event) {
			var shootId = getSelectedRow();
			if(shootId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(shootId)
		},
		saveOrUpdate: function (event) {
			var url = vm.shoot.shootId == null ? "picApproval/shoot/save" : "picApproval/shoot/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.shoot),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var shootIds = getSelectedRows();
			if(shootIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "picApproval/shoot/delete",
                    contentType: "application/json",
				    data: JSON.stringify(shootIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(shootId){
			$.get(baseURL + "picApproval/shoot/info/"+shootId, function(r){
                vm.shoot = r.shoot;
            });
		},

        //审批提交
        doApprovalOption: function(){

            $("#pic_process").modal('hide');

            console.log(vm.processView);

            $.ajax({
                type: "POST",
                url: baseURL + "picApproval/shoot/process",
                contentType: "application/json",
                data: JSON.stringify(vm.processView),
                success: function(r){
                    if(r.code === 0){
                        console.log(vm.processView);
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'processStatus': vm.q.processStatus,
                    'shootType': vm.q.shootType,
                    'userName': vm.q.userName
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});